<?php

namespace App\Http\Controllers\Admin\Site;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Custom\HotWord;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class HotWordController extends Controller
{
    use AdminControllerBuildTrait;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $query = HotWord::orderBy('created_at', 'desc');

            $hotWords = $query->paginate($request->query('pageSize', 7));

            $data = [];
            foreach ($hotWords as $hotWord) {
                $item   = [
                    'id'         => $hotWord->id,
                    'name'       => $hotWord->name,
                    'sort'       => $hotWord->sort,
                    'show'       => '热门搜索',
                    'link'       => $hotWord->link,
                    'editor_url' => route('admin.hot.word.edit', [$hotWord->id, 'page' => $request->input('page')]),
                    'dele_url'   => route('admin.hot.word.destroy',
                        [$hotWord->id, 'page' => $request->input('page')]),
                ];
                $data[] = $item;
            }
            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $hotWords->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $hotWords->total(),
                ],
            ]);
        }
        return view('admin/site/hot-word/index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin/site/hot-word/edit')->with('form', route('admin.hot.word.index'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make(
            $request->except(['_token']),
            [
                'name'     => 'required',
                'sort'     => 'required|numeric',
                'classify' => 'required',
                'link'     => 'required',
            ]
        );
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator);
        }
        $data             = $request->only(['name', 'sort', 'link']);// 只取这些数据
        $data['position'] = $request->input('classify');
        $hotWord          = new HotWord();
        $hotWord->create($data);

        return $this->index($request);

    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $hot = HotWord::findOrNew($id);

        return view('admin/site/hot-word/edit')->with('hot', $hot)->with('form',
            route('admin.hot.word.update', [$hot->id, 'page' => $request->input('page')]));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make(
            $request->except(['_token']),
            [
                'name'     => 'required',
                'sort'     => 'required|numeric',
                'classify' => 'required',  //显示位置
                'link'     => 'required',
            ]
        );
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator);
        }
        $data             = $request->only(['name', 'sort', 'link']);// 只取这些数据
        $data['position'] = $request->input('classify');

        $hotWord = HotWord::findOrFail($id);
        $hotWord->update($data);

        return $this->index($request);
    }

    /**
     * 单个删除
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        $hotWord = HotWord::findOrFail($id);
        $hotWord->delete();

        return $this->index($request);
    }

    /**
     * 批量删除  循环遍历id进行删除
     * @param Request $request
     * @return mixed
     * @throws ModelNotFoundException
     * @throws \Exception
     */
    public function batchRemove(Request $request)
    {
        try {
            return \DB::transaction(function () use ($request) {
                $arr = $request->input('ids');
                foreach ($arr as $hot_id) {
                    $hotWord = HotWord::findOrFail($hot_id);
                    $hotWord->delete();
                }
                return $this->index($request);
            });
        } catch (ModelNotFoundException $e) {
            throw $e;
        }
    }

    /**
     * 获取动态显示位置
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPosition()
    {
        //TODO 目前是假数据
        $data['data'][] = [
            'code' => '1',
            'text' => '热门搜索',
        ];
        return response()->json($data);
    }
}
